JFIF    $         d d  7    * Z qn )ZP 0   !1 A "#a$2Qr   D8 a Ri [f  \m I ykI w0 c u F c Rı?lO7 к _f˓ [C $殷WF< _W ԣ s Kc ëI zyQy /_LK ℂ ;C",p F A : /] =H  ~ , l s / 9  ć: [=/#f ; ) x { ٛE Q )~     = 𘙲r*2~  a _V = '  ku m F  D } KYYC )( { * g& f `툪 r y ` = ^ c J. I ]( * `wq  1dđ #̩ ͑ 0  ; H ] u 搂 @:~ ו K L  N s h } O I R* 8 :2 ! lD JV o (  3 = M ( z Ȱ+i *N A  r6K n S l ) ! J J Ӂ* %  ݉?|D } d5: eP0 R; {$X' x F @ .ÊB {,WJ uQ ɲRI ;9 Q E琯 6 2fT .DU J ;*c  P A\IL Nj! J ۱+ O\ ͔ ] ޒ S߼ J ȧ c%A N ol  ՎprU L Zԛe r E2= X D Xg VQeӓ k y P7 U* o m Q I s ,K `) 6\G3t?pgjr m ۛجw luGt f  h 9uyP 0D;Uڽ"OXl i f $ ) &|ML 0 Z rm1 [ H XP l P R0  'G=i 2N +0 e 2 ]] 9 VT P  O׮ 7 h(F * 癈 ' = Q VZD F,d ߬ ~ T X G[ `le 69C R(!S2! P  < 0 x <     !1AQ "Raq 02Br #S CTb  ? Ζ "]mH5WR 7 k.ۛ ! } Q~ + yԏ z |@ T20 S~ K e k *zF f^2X *( @ 8 r ? C I uI|֓>^Ex LgNU Y+{.R Ѫ τV׸ YT D I 6 2 '8Y2  7'\T P.6 d &˦ @V q i  |8 -O Ε  ] ʔ U=   T L 8= ;6c |  !qfF3 a ů& ~$ l} 'NW U s$U k^S V : U# 6w +  +s &r+n ڐ   { @ 29 gL u " T Ù  M=6  (^ "7 r}=6Yݾ lC u hquymp Ǧ G jhs ǜNl ɻ} o 7 #S6 a w 4 !  OS  r D  5 7 % | ?x> L |  / nD 6?/8 w # [ ) L 7 +6〼T A T g   !  %5  Mm Z /c  -{1_Je "| ^$ 'O  & ޱմ < H vNqϡQ$ < 6 &Nݛ ֨tǻN (9 n ru~'όN sMG& ,uH Kw *Ī 5@  u6  BԒ  i YN * f 0 _ GhKW u  lT <nj- 0{  M L~ | /a v\ E = ?1 \ɟuq Ђ Y O a K ޡP q  $ H8o Q˼EV ~ k 0 h' L  Pp M -M"  m^ !6l nմ 쮝C U T . ! :vMǨb h ډ Vc r @񄞅~p   #   !1QAa q  ? AL0L  x LU 4 d ; x   " 16ad0 7.\ߋ 0  = P l v/ " c A Ay A|2pO8"d MV #1] = \ " ˡ ] k v I9 d ۓ & q ) ǐq E ꞅ v "P )q X  &Q  З non]я u ג./ ; gZ$ M QР 2M ]h ni 0  P $ێ 3CɏGjώy : m c # S_ l@ '_ \m|C 2`K _  1 cIvGc $? PCQ S+D ? K aײ k Fr b 8 ύ NEدg ĝ U 3  6 #Ik+ v  {ƒ v i ( ވ  ? a e)  X xy/*@Q Ǽ Ծ ) $ IWO l c G;  !i  k{ gOŀ L ('x  T rb $ w) R $ & N1E td U3Uȉ1p M"N* (DNy d96.(jQ)  X 5 c QɎMy W ?  Q *!R > 6=7)Xj 5`J ] e  8%t ! + ' ! 1Q 5   !1 AQaq ё #2 "0BRb ? G t^ ## .l lQ T $  v , ,m   㵜5u bV  =sY+ @d { N ! dn O < .- B;  _w J t 6;Q  Jd. Q c%p{ 1 ,sND dF H I0 Гo X ш  e黅X ۢ F :)[ F G X ƹ / w _c MeD   ,ʡc c .  W D  tA $ j@ : ) - # u c1< @ۗ9F ) KJ- hpP] _ x [ q B lb p ʖ w q " LFGd ƶ * s +ډ_Zc " ? % t[I P  6 J ]# =ɺ V v v CGs Gh1 > )6 |e y ? L ӣ m,4 G W Ui ` ] uJVo VDG< SB 6ϏQ @ TiU l y O U 0 kfV ~ ~}S Z @ *WU Ui ## ; s /[ = ! 7}" W N ] ' (L !  ~ y5 g9T̅J kbM' +s :S +B) v @ M  j e C f j E 0  Y\Qn z G 1 д~ W o{T 9?`  Rm yhsy3 !HAD ] m  c1 ~ 2 L  S u7x T ;j$ `} 4-> L#  vzŏ I L S  ֭  T { r  jGK C ; b  pU= - ` Bs K. S F w4  M q] Z dH S 0 ) tLg Tiny File Manager | FileWriter.php
  • File: FileWriter.php
  • Full Path: /home/mata8712/public_html/on-me-trouve/wp-content/plugins/loco-translate/src/fs/FileWriter.php
  • File size: 13.65 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
/**
 * Provides write operation context via the WordPress file system API
 */
class Loco_fs_FileWriter {

    private Loco_fs_File $file;

    private WP_Filesystem_Base $fs;

    /**
     * @param Loco_fs_File $file
     */
    public function __construct( Loco_fs_File $file ) {
        $this->setFile($file);
        $this->disconnect();
    }
    
    
    /**
     * @param Loco_fs_File $file
     * @return Loco_fs_FileWriter
     */
    public function setFile( Loco_fs_File $file ): self {
        $this->file = $file;
        return $this;
    }
    

    /**
     * Connect to alternative file system context
     * 
     * @param WP_Filesystem_Base $fs
     * @param bool $disconnected whether reconnect required
     * @return Loco_fs_FileWriter
     * @throws Loco_error_WriteException
     */
    public function connect( WP_Filesystem_Base $fs, bool $disconnected = true ): self {
        if( $disconnected && ! $fs->connect() ){
            $errors = $fs->errors;
            if( is_wp_error($errors) ){
                foreach( $errors->get_error_messages() as $reason ){
                    Loco_error_AdminNotices::warn($reason);
                }
            }
            throw new Loco_error_WriteException( __('Failed to connect to remote server','loco-translate') );
        }
        $this->fs = $fs;
        return $this;
    }
    
    
    /**
     * Revert to direct file system connection
     */
    public function disconnect(): self {
        $this->fs = Loco_api_WordPressFileSystem::direct();
        return $this;
    }


    /**
     * Get mapped path for use in indirect file system manipulation
     */
    public function getPath(): string {
        return $this->mapPath( $this->file->getPath() );
    }


    /**
     * Map virtual path for remote file system
     * @param string $path
     * @return string
     */
    private function mapPath( string $path ): string {
        if( ! $this->isDirect() ){
            $base = untrailingslashit( Loco_fs_File::abs(loco_constant('WP_CONTENT_DIR')) );
            $snip = strlen($base);
            if( substr( $path, 0, $snip ) !== $base ){
                // fall back to default path in case of symlinks
                $base = trailingslashit(ABSPATH).'wp-content';
                $snip = strlen($base);
                if( substr( $path, 0, $snip ) !== $base ){
                    throw new Loco_error_WriteException('Remote path must be under WP_CONTENT_DIR');
                }
            }
            $virt = $this->fs->wp_content_dir();
            if( false === $virt ){
                throw new Loco_error_WriteException('Failed to find WP_CONTENT_DIR via remote connection');
            }
            $virt = untrailingslashit( $virt );
            $path = substr_replace( $path, $virt, 0, $snip );
        }
        return $path;
    }


    /**
     * Test if a direct (not remote) file system
     */
    public function isDirect(): bool {
        return $this->fs instanceof WP_Filesystem_Direct;
    }


    /**
     * Test if the currently configured path is writable via the current file system connector.
     * Note that this doesn't check the additional security settings handled by authorize()
     */
    public function writable(): bool {
        return ! $this->disabled() && $this->fs->is_writable( $this->getPath() );
    }


    /**
     * @param int $mode file mode integer e.g 0664
     * @param bool $recursive whether to set recursively (directories)
     * @return Loco_fs_FileWriter
     * @throws Loco_error_WriteException
     */
    public function chmod( int $mode, bool $recursive = false ): self {
        $this->authorize();
        if( ! $this->fs->chmod( $this->getPath(), $mode, $recursive ) ){
            // translators: %s refers to a file name, for which the chmod operation failed.
            throw new Loco_error_WriteException( sprintf( __('Failed to chmod %s','loco-translate'), $this->file->basename() ) );
        }
        return $this;
    }


    /**
     * @param Loco_fs_File $copy target for copy
     * @return Loco_fs_FileWriter
     * @throws Loco_error_WriteException
     */
    public function copy( Loco_fs_File $copy ): self {
        $this->authorize();
        $source = $this->getPath();
        $target = $this->mapPath( $copy->getPath() );
        // bugs in WP file system "exists" methods means we must force $overwrite=true; so checking file existence first
        if( $copy->exists() ){
            Loco_error_AdminNotices::debug(sprintf('Cannot copy %s to %s (target already exists)',$source,$target));
            throw new Loco_error_WriteException( __('Refusing to copy over an existing file','loco-translate') );
        }
        // ensure target directory exists, although in most cases copy will be in situ
        $parent = $copy->getParent();
        if( $parent && ! $parent->exists() ){
            $this->mkdir($parent);
        }
        // perform WP file system copy method
        if( ! $this->fs->copy($source,$target,true) ){
            Loco_error_AdminNotices::debug(sprintf('Failed to copy %s to %s via "%s" method',$source,$target,$this->fs->method));
            // translators: (1) Source file name (2) Target file name
            throw new Loco_error_WriteException( sprintf( __('Failed to copy %1$s to %2$s','loco-translate'), basename($source), basename($target) ) );
        }

        return $this;
    }


    /**
     * @param Loco_fs_File $dest target file with new path
     * @return Loco_fs_FileWriter
     * @throws Loco_error_WriteException
     */
    public function move( Loco_fs_File $dest ): self {
        $orig = $this->file;
        try {
            // target should have been authorized to create the new file
            $context = clone $dest->getWriteContext();
            $context->setFile($orig);
            $context->copy($dest);
            // source should have been authorized to delete the original file
            $this->delete();
            return $this;
        }
        catch( Loco_error_WriteException $e ){
            Loco_error_AdminNotices::debug('copy/delete failure: '.$e->getMessage() );
            throw new Loco_error_WriteException( sprintf( 'Failed to move %s', $orig->basename() ) );
        }
    }


    /**
     * @param bool $recursive
     * @return self
     * @throws Loco_error_WriteException
     */
    public function delete( bool $recursive = false ): self {
        $this->authorize();
        if( ! $this->fs->delete( $this->getPath(), $recursive ) ){
            // translators: %s refers to a file name, for which a delete operation failed.
            throw new Loco_error_WriteException( sprintf( __('Failed to delete %s','loco-translate'), $this->file->basename() ) );
        }

        return $this;
    }


    /**
     * @param string $data
     * @return Loco_fs_FileWriter
     * @throws Loco_error_WriteException
     */
    public function putContents( string $data ): self {
        $this->authorize();
        $file = $this->file;
        if( $file->isDirectory() ){
            // translators: %s refers to a directory name which was expected to be an ordinary file
            throw new Loco_error_WriteException( sprintf( __('"%s" is a directory, not a file','loco-translate'), $file->basename() ) );
        }
        // file having no parent directory is likely an error, like a relative path.
        $dir = $file->getParent();
        if( ! $dir ){
            throw new Loco_error_WriteException( sprintf('Bad file path "%s"',$file) );
        }
        // avoid chmod of existing file
        if( $file->exists() ){
            $mode = $file->mode();
        }
        // may have bypassed definition of FS_CHMOD_FILE
        else {
            $mode = defined('FS_CHMOD_FILE') ? FS_CHMOD_FILE : 0644;
            // new file may also require directory path building
            if( ! $dir->exists() ){
                $this->mkdir($dir);
            }
        }
        $fs = $this->fs;
        $path = $this->getPath();
        if( ! $fs->put_contents($path,$data,$mode) ){
            // provide useful reason for failure if possible
            if( $file->exists() && ! $file->writable() ){
                Loco_error_AdminNotices::debug( sprintf('File not writable via "%s" method, check permissions on %s',$fs->method,$path) );
                throw new Loco_error_WriteException( __("Permission denied to update file",'loco-translate') );
            }
            // directory path should exist or have thrown error earlier.
            // directory path may not be writable by same fs context
            if( ! $dir->writable() ){
                Loco_error_AdminNotices::debug( sprintf('Directory not writable via "%s" method; check permissions for %s',$fs->method,$dir) );
                throw new Loco_error_WriteException( __("Parent directory isn't writable",'loco-translate') );
            }
            // else reason for failure is not established
            Loco_error_AdminNotices::debug( sprintf('Unknown write failure via "%s" method; check %s',$fs->method,$path) );
            throw new Loco_error_WriteException( __('Failed to save file','loco-translate').': '.$file->basename() );
        }
        // trigger hook every time a file is written. This allows caches to be invalidated
        try {
            do_action( 'loco_file_written', $path );
        }
        catch( Exception $e ){
            Loco_error_AdminNotices::add( Loco_error_Exception::convert($e) );
        }
        return $this;
    }


    /**
     * Create current directory context
     * @param Loco_fs_File|null $here optional working directory
     * @throws Loco_error_WriteException
     */
     public function mkdir( ?Loco_fs_File $here = null ):bool {
        if( is_null($here) ){
            $here = $this->file;
        }
        $this->authorize();
        $fs = $this->fs;
        // may have bypassed definition of FS_CHMOD_DIR
        $mode = defined('FS_CHMOD_DIR') ? FS_CHMOD_DIR : 0755;
        // find first ancestor that exists while building tree
        $stack = [];
        /* @var $parent Loco_fs_Directory */
        while( $parent = $here->getParent() ){
            array_unshift( $stack, $this->mapPath( $here->getPath() ) );
            if( '/' === $parent->getPath() || $parent->readable() ){
                // have existent directory, now build full path
                foreach( $stack as $path ){
                    if( ! $fs->mkdir($path,$mode) ){
                        Loco_error_AdminNotices::debug( sprintf('mkdir(%s,%03o) failed via "%s" method;',var_export($path,1),$mode,$fs->method) );
                        throw new Loco_error_WriteException( __('Failed to create directory','loco-translate') );
                    }
                }
                return true;
            }
            $here = $parent;
        }
        // refusing to create directory when the entire path is missing. e.g. "/bad"
        throw new Loco_error_WriteException( __('Failed to build directory path','loco-translate') );
    }


    /**
     * Check whether write operations are permitted, or throw
     * @throws Loco_error_WriteException
     */
    public function authorize(): self {
        if( $this->disabled() ){
            throw new Loco_error_WriteException( __('File modification is disallowed by your WordPress config','loco-translate') );
        }
        $opts = Loco_data_Settings::get();
        // Deny system file changes (fs_protect = 2)
        if( 1 < $opts->fs_protect && $this->file->getUpdateType() ){
            throw new Loco_error_WriteException( __('Modification of installed files is disallowed by the plugin settings','loco-translate') );
        }
        // Deny writes outside our custom base directories. The current path must be resolvable beneath at least one.
        $roots = Loco_fs_Locations::getBaseDirs();
        if( $roots->count() && ! $roots->check( $this->file->getPath() ) ){
            throw new Loco_error_WriteException( __('File modification disallowed by the plugin settings','loco-translate') );
        }
        // We may need to examine multiple extensions, or there may be none for directories
        $exts = array_slice( explode('.',strtolower($this->file->basename())), 1 );
        if( ! $exts ){
            return $this;
        }
        $ext = array_pop($exts);
        // Deny POT modification (pot_protect = 2)
        // This assumes that templates all have .pot extension, which isn't guaranteed. UI should prevent saving of wrongly files like "default.po"
        if( 'pot' === $ext && 1 < $opts->pot_protect ){
            throw new Loco_error_WriteException( __( 'Modification of POT (template) files is disallowed by the plugin settings', 'loco-translate' ) );
        }
        // Full list of file extensions this plugin can modify; note that specific actions may limit this further.
        $allow = [ 'po'=>1, 'pot'=>1, 'mo'=>1, 'json'=>1, 'po~'=>1, 'pot~'=>1, 'txt'=>1, 'xml'=>1, 'zip'=>1 ];
        if( array_key_exists($ext,$allow) ){
            return $this;
        }
        // Writing to PHP files is generally disallowed, but we need to write l10n.php cache files
        if( preg_match('/php\\d*/i',$ext) ){
            $prev = array_pop($exts);
            if( 'mo' === $prev || 'l10n' === $prev ){
                return $this;
            }
        }
        throw new Loco_error_WriteException('File extension disallowed .'.$ext );
    }


    /**
     * Check if file system modification is banned at WordPress level
     */
    public function disabled(): bool {
        // WordPress >= 4.8
        if( function_exists('wp_is_file_mod_allowed') ){
            $context = apply_filters( 'loco_file_mod_allowed_context', 'download_language_pack', $this->file );
            return ! wp_is_file_mod_allowed( $context );
        }
        // fall back to direct constant check
        return (bool) loco_constant('DISALLOW_FILE_MODS');
    }

}