]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/libraries/send2kindle/storage.php
add mobi and pdf to routing
[github/wallabag/wallabag.git] / inc / 3rdparty / libraries / send2kindle / storage.php
1 <?php
2 /**
3 * Storage for news2Kindle
4 * @author jwest <jwest@jwest.pl>
5 */
6 class Storage {
7
8 /**
9 * Newspapper name
10 * @var string
11 */
12 private $_name;
13
14 /**
15 * Default dir
16 * @var string
17 */
18 private $_default_dir = 'data/';
19
20 /**
21 * Info about newspapper
22 * @var array
23 */
24 private $_info;
25
26 /**
27 * Prepare storage
28 * @param string $newspapper
29 * @param string $default_dir
30 */
31 public function __construct($newspapper, $default_dir = NULL)
32 {
33 $this->_name = $newspapper;
34
35 if ( $default_dir !== NULL )
36 {
37 $this->_default_dir = $default_dir;
38 }
39
40 $this->_info = $this->_get_info();
41 }
42
43 /**
44 * save info before exit from script
45 */
46 public function __destruct()
47 {
48 $this->_save_info($this->_info);
49 }
50
51 /**
52 * Get path
53 * @param bool $newspapper path to newspapper
54 * @return string path to repository
55 */
56 public function get_path($newspapper = true)
57 {
58 $path = $this->_default_dir;
59
60 if ( $newspapper )
61 {
62 $path .= $this->_name . '/';
63 }
64
65 return $path;
66 }
67
68 /**
69 * Get info about newspapper
70 * @return array newspapper info
71 */
72 private function _get_info()
73 {
74 $content = unserialize( $this->_get_file('info.dat') );
75
76 if( $content === FALSE )
77 {
78 $content = (object) array
79 (
80 'images_count' => 0,
81 'articles_count' => 0,
82 'images' => array(),
83 );
84
85 $this->_save_info($content);
86 }
87
88 return $content;
89 }
90
91 /**
92 * Get info key
93 * @param $key
94 * @return mixed
95 */
96 public function info($key)
97 {
98 return $this->_info->$key;
99 }
100
101 /**
102 * Save info
103 * @param array $info info from class
104 */
105 private function _save_info($info)
106 {
107 $this->_save_file( 'info.dat', serialize($info) );
108 }
109
110 /**
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
115 */
116 public function save_image($image, $name = NULL)
117 {
118 if ( $name === NULL )
119 {
120 $image_name = (string) $this->_info->images_count;
121
122 for ($i=strlen($image_name); $i<6; $i++)
123 {
124 $image_name = '0'.$image_name;
125 }
126
127 $this->_info->images_count++;
128 $this->_info->images[$this->_info->images_count] = $image_name;
129
130 $name = $image_name;
131 }
132
133 $this->_save_file($name, $image);
134
135 return $this->_default_dir . $this->_name . '/' . $name;
136 }
137
138 /**
139 * Get image
140 * @param string $name image name
141 * @return resource
142 */
143 public function get_image($name)
144 {
145 return $this->_get_file($name);
146 }
147
148 /**
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
155 */
156 public function add_content($id, $title, $content, $url, $website)
157 {
158 $articles = unserialize( $this->_get_file('articles.dat') );
159
160 $articles[$id] = (object) array
161 (
162 'id' => $id,
163 'title' => $title,
164 'content' => $content,
165 'url' => $url,
166 'website' => $website,
167 );
168
169 $this->_save_file( 'articles.dat', serialize($articles) );
170 }
171
172 /**
173 * Get articles contents
174 * @param string $file_name
175 * @param string $file_content
176 */
177 public function get_contents()
178 {
179 return unserialize( $this->_get_file( 'articles.dat' ) );
180 }
181
182 /**
183 * Get file content
184 * @param string $file_name
185 * @param string $file_content
186 */
187 private function _save_file($file_name, $file_content)
188 {
189 if( ! file_exists( $this->_default_dir . $this->_name ) )
190 {
191 mkdir( $this->_default_dir . $this->_name, 0777, TRUE );
192 }
193
194 file_put_contents($this->_default_dir . $this->_name . '/' . $file_name, $file_content);
195 }
196
197 /**
198 * Get file content
199 * @param string $file_name
200 * @param string $default_file_content (if file not exists)
201 * @return string file content
202 */
203 private function _get_file($file_name, $default_file_content = NULL)
204 {
205 $content = @file_get_contents($this->_default_dir . $this->_name . '/' . $file_name);
206
207 if ($content === FALSE)
208 {
209 return $default_file_content;
210 }
211
212 return $content;
213 }
214
215 /**
216 * Clean newspapper after convert to mobi
217 */
218 public function clean()
219 {
220 $files_to_remove = glob($this->_default_dir . $this->_name . '/*');
221
222 foreach ( $files_to_remove as $file )
223 {
224 unlink( $file );
225 }
226
227 rmdir( $this->_default_dir . $this->_name );
228 }
229
230 }