]>
git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/send2kindle/storage.php
3 * Storage for news2Kindle
4 * @author jwest <jwest@jwest.pl>
18 private $_default_dir = 'data/';
21 * Info about newspapper
28 * @param string $newspapper
29 * @param string $default_dir
31 public function __construct($newspapper, $default_dir = NULL)
33 $this->_name
= $newspapper;
35 if ( $default_dir !== NULL )
37 $this->_default_dir
= $default_dir;
40 $this->_info
= $this->_get_info();
44 * save info before exit from script
46 public function __destruct()
48 $this->_save_info($this->_info
);
53 * @param bool $newspapper path to newspapper
54 * @return string path to repository
56 public function get_path($newspapper = true)
58 $path = $this->_default_dir
;
62 $path .= $this->_name
. '/';
69 * Get info about newspapper
70 * @return array newspapper info
72 private function _get_info()
74 $content = unserialize( $this->_get_file('info.dat') );
76 if( $content === FALSE )
78 $content = (object) array
81 'articles_count' => 0,
85 $this->_save_info($content);
96 public function info($key)
98 return $this->_info
->$key;
103 * @param array $info info from class
105 private function _save_info($info)
107 $this->_save_file( 'info.dat', serialize($info) );
111 * Save image in storage
112 * @param resource $image
113 * @param string $name image name, if null create auto name
114 * @return string image name
116 public function save_image($image, $name = NULL)
118 if ( $name === NULL )
120 $image_name = (string) $this->_info
->images_count
;
122 for ($i=strlen($image_name); $i<6; $i++
)
124 $image_name = '0'.$image_name;
127 $this->_info
->images_count++
;
128 $this->_info
->images
[$this->_info
->images_count
] = $image_name;
133 $this->_save_file($name, $image);
135 return $this->_default_dir
. $this->_name
. '/' . $name;
140 * @param string $name image name
143 public function get_image($name)
145 return $this->_get_file($name);
149 * Put article contents
150 * @param string $id unique id for article
151 * @param string $title
152 * @param string $content article content
153 * @param string $url url for article
154 * @param object $website
156 public function add_content($id, $title, $content, $url, $website)
158 $articles = unserialize( $this->_get_file('articles.dat') );
160 $articles[$id] = (object) array
164 'content' => $content,
166 'website' => $website,
169 $this->_save_file( 'articles.dat', serialize($articles) );
173 * Get articles contents
174 * @param string $file_name
175 * @param string $file_content
177 public function get_contents()
179 return unserialize( $this->_get_file( 'articles.dat' ) );
184 * @param string $file_name
185 * @param string $file_content
187 private function _save_file($file_name, $file_content)
189 if( ! file_exists( $this->_default_dir
. $this->_name
) )
191 mkdir( $this->_default_dir
. $this->_name
, 0777, TRUE );
194 file_put_contents($this->_default_dir
. $this->_name
. '/' . $file_name, $file_content);
199 * @param string $file_name
200 * @param string $default_file_content (if file not exists)
201 * @return string file content
203 private function _get_file($file_name, $default_file_content = NULL)
205 $content = @file_get_contents($this->_default_dir
. $this->_name
. '/' . $file_name);
207 if ($content === FALSE)
209 return $default_file_content;
216 * Clean newspapper after convert to mobi
218 public function clean()
220 $files_to_remove = glob($this->_default_dir
. $this->_name
. '/*');
222 foreach ( $files_to_remove as $file )
227 rmdir( $this->_default_dir
. $this->_name
);