Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.51 KB | None | 0 0
  1. <?php
  2. use PHPImageWorkshop\ImageWorkshop;
  3. class mainContent {
  4.  
  5. // Metoda CSS
  6. function myCSS() {
  7. return '
  8. #apigui .block {
  9. background: white;
  10. margin: 5px 10px;
  11. box-shadow: -1px 1px 5px #CCC;
  12. border: 1px solid #CCC;
  13. border-radius: 5px;
  14. width: 35%;
  15. color: black;
  16. padding: 15px;
  17. display: inline-block;
  18. text-align: left;
  19. font-size: 12px;
  20. padding-right: 100px;
  21. position: relative;
  22. }
  23. #apigui .block .description {
  24. display:block;
  25. padding-right:6px;
  26. }
  27. #apigui .block input[type~=text] {
  28. width: 100% !important;
  29. }
  30. #apigui .block input[type~=radio] {
  31. margin: 3px 5px;
  32. }
  33. #apigui .block select {
  34. margin: 5px 0px;
  35. width: 100%;
  36. }
  37. #apigui .block h2 {
  38. font-weight: normal !important;
  39. }
  40. #apigui .warning {
  41. font-weight: bold;
  42. color: red;
  43. }
  44. ';
  45. }
  46.  
  47. //Podstrony
  48. function subpages() {
  49. return array(
  50. 'Import z adresu' => 1,
  51. 'Import z katalogu' => 2,
  52. );
  53. }
  54.  
  55. //Wywolanie podstron
  56. function init($get) {
  57. switch($get) {
  58. case 1: $this->fromUrl(); break;
  59. case 2: $this->fromDir(); break;
  60. default: $this->fromUrl();
  61. }
  62. }
  63.  
  64. function fromUrl() {
  65. if(isset($_POST['import'])) {
  66. $url = $_POST['url'];
  67. $parse_url = parse_url($url);
  68. $dir = '../upload/';
  69. $min_width = $_POST['min_width'];
  70. $min_height = $_POST['min_height'];
  71. $cropp = $_POST['cropp'];
  72. $date = date('Y-m-d H:i:s');
  73. $settings = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_settings` WHERE `id`='1'"));
  74.  
  75. $ch = curl_init();
  76. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  77. curl_setopt($ch, CURLOPT_TIMEOUT, 4);
  78. curl_setopt($ch, CURLOPT_URL, $url);
  79. $html = curl_exec($ch);
  80. $errors = 0;
  81. $images = 0;
  82. set_time_limit(60);
  83.  
  84. if(preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $matches))
  85. {
  86. foreach($matches[1] as $img) {
  87. if(strpos($img, "http://") === FALSE) {
  88. $ex = explode("/",$img);
  89. if(reset($ex)==NULL) $img = $parse_url['scheme'].'://'.$parse_url['host'].$img;
  90. else $img = $parse_url['scheme'].'://'.$parse_url['host'].'/'.$img;
  91. }
  92.  
  93. list($width, $height, $type, $attr) = getimagesize($img);
  94. if($width>$min_width && $height>$min_height) {
  95. $info = $this->file_info($img);
  96. if(!file_exists($dir.$info['basename'])) {
  97. $import = file_put_contents($dir.$info['basename'],file_get_contents($img));
  98. if($import) {
  99. if($cropp>0) $this->cropp($dir.$info['basename'], $cropp, $width, $height, $dir);
  100. if($settings['watermark']) {
  101. if(strpos($settings['watermark'], "http://") === FALSE) $watermark = '../'.$settings['watermark'];
  102. else $watermark = $settings['watermark'];
  103. $this->watermark($dir.$info['basename'], $watermark, $dir);
  104. }
  105. $query = mysql_query("INSERT INTO `tentego_img` (`title`,`src`,`type`,`owner`,`cat`,`date`,`rel_date`,`source`,`is_waiting`) VALUES ('".$info['filename']."','".$info['basename']."','img',1,1,'$date',0,'-----',1)");
  106. }
  107. if(!$import || !$query) $errors++;
  108. else $images++;
  109. }
  110. }
  111. }
  112. if($errors) kernel::make_notify("Nie udało się zaimportować obrazków");
  113. else kernel::make_notify("Zaimportowano ".$images." obrazków");
  114. }
  115. else kernel::make_notify("Nie znaleziono żadnych obrazków");
  116. }
  117.  
  118. echo "<script type=\"text/javascript\">
  119. $(document).ready(function(){
  120. $('input[name$=\"import\"]').click( function() { $('img[class$=\"loading\"]').attr('style', 'visibility: visible;'); });
  121. });
  122. </script>";
  123.  
  124. echo '<div id="apigui">
  125. <form method="post" action="?go=getimg&feature=1">
  126. <div id="options">
  127. '.($this->checkExtension('curl') ? '<input type="submit" value="Importuj" name="import">' : '<input type="submit" value="Importuj" name="import" disabled> <span class="warning">Rozszerzenie <em>curl</em> nie jest włączone na serwerze!</span>').'
  128. <img src="apps/getimg/loading.gif" class="loading" style="visibility: hidden;" valign="middle" />
  129. </div>
  130. <div class="block">
  131. <h2>Minimalna szerokość</h2>
  132. <span class="description">Określa minimalną szerokość obrazków, które zostaną zaimportowane.</span>
  133. <input type="text" name="min_width" value="300" />
  134. </div>
  135. <div class="block">
  136. <h2>Minimalna wysokość</h2>
  137. <span class="description">Określa minimalną wysokość obrazków, które zostaną zaimportowane.</span>
  138. <input type="text" name="min_height" value="200" />
  139. </div>
  140. <div class="block">
  141. <h2>Przycięcie</h2>
  142. <span class="description">Wartość w pikselach, która zostanie odcięta od dołu obrazka.</span>
  143. <input type="text" name="cropp" value="0" />
  144. </div>
  145. <div class="block">
  146. <h2>Adres URL</h2>
  147. <span class="description">Adres do strony, z której mają zostać pobrane obrazki <em>(bez / na końcu)</em>.</span>
  148. <input type="text" name="url" value="http://" />
  149. </div>
  150. </form>
  151. </div>';
  152. }
  153.  
  154. function fromDir() {
  155. if(isset($_GET['import'])) {
  156. $file_types = array(1=>'jpg','jpeg','gif','png','JPG','JPEG','GIF','PNG');
  157. $settings = mysql_fetch_array(mysql_query("SELECT * FROM `tentego_settings` WHERE `id`='1'"));
  158. $date = date('Y-m-d H:i:s');
  159. $errors = 0;
  160. $loop = 0;
  161.  
  162. if(is_dir('../'.$_GET['import'])) {
  163. $dir = opendir('../'.$_GET['import']);
  164. while(false !== ($file = readdir($dir))) {
  165. $file_name = date('YmdHis').$loop.'uid1';
  166. $info = pathinfo($file);
  167. if($file != '.' && $file != '..' && array_search($info['extension'],$file_types))
  168. $przeniesienie = rename('../'.$_GET['import'].'/'.$file, '../upload/'.$file_name.'.'.$info['extension']);
  169. if($przeniesienie) {
  170. if($settings['watermark']) {
  171. if(strpos($settings['watermark'], "http://") === FALSE) $watermark = '../'.$settings['watermark'];
  172. else $watermark = $settings['watermark'];
  173. $this->watermark('../upload/'.$file_name.'.'.$info['extension'], $watermark, '../upload/');
  174. }
  175. $query = mysql_query("INSERT INTO `tentego_img` (`title`,`src`,`type`,`owner`,`cat`,`date`,`rel_date`,`source`,`is_waiting`) VALUES ('".$info['filename']."','".$file_name.'.'.$info['extension']."','img',1,1,'$date',0,'-----',1)");
  176. ### USTAWIANIE OSTATNIEGO ID ###
  177. $last_id = mysql_insert_id();
  178. ### KONIEC OSTATNIEGO ID ###
  179.  
  180. ### FACEBOOK #
  181. require_once('_core/fbapi/autoload.php');
  182. $fb = new Facebook\Facebook([
  183. 'app_id' => '',
  184. 'app_secret' => '',
  185. 'default_graph_version' => 'v2.11',
  186. ]);
  187. $pageAccessToken ='';
  188. $page_id = 123104298384383;
  189. ### KONIEC FACEBOOK ###
  190.  
  191. ### UDOSTĘPNIANIE FB ###
  192. $linkData = [
  193. 'message' => $info['filename'],
  194. 'picture' => "https://memuje.pl/upload/".$file_name,
  195. 'url' => "https://memuje.pl/img/".$last_id."/".$$info['filename']."/",
  196. 'link' => "https://memuje.pl/img/".$last_id."/".$$info['filename']."/",
  197. ];
  198. try {
  199. $response = $fb->post($page_id.'/feed', $linkData, $pageAccessToken);
  200. } catch(Facebook\Exceptions\FacebookResponseException $e) {
  201. echo 'Graph returned an error: '.$e->getMessage();
  202. exit;
  203. } catch(Facebook\Exceptions\FacebookSDKException $e) {
  204. echo 'Facebook SDK returned an error: '.$e->getMessage();
  205. exit;
  206. }
  207. $graphNode = $response->getGraphNode();
  208.  
  209. ### KONIEC UDOSTĘPNIANIA ###
  210.  
  211. if(!$query) $errors++;
  212. }
  213. $loop++;
  214. }
  215. }
  216. if(!$errors) kernel::make_notify("Poprawnie zaimportowano obrazki");
  217. else kernel::make_notify("Nie udało się zaimportować obrazków");
  218. }
  219. echo '<div id="apigui">
  220. <div id="options">Wybierz folder z katalogu głównego, z którego mają zostać zaimportowane obrazy.</div>
  221. <table>
  222. <tbody>';
  223. $this->cat_list('../');
  224. $cats = 0;
  225. for( $x = 0, $cnt = count($this->katalogi); $x < $cnt; $x++ ) {
  226. if($this->katalogi[$x]!='_core' && $this->katalogi[$x]!='_js' && $this->katalogi[$x]!='_themes' && $this->katalogi[$x]!='admin' && $this->katalogi[$x]!='upload') {
  227. echo '<tr><td>'.$this->katalogi[$x].'</td> <td><em>'.$this->numberOfPictures('../'.$this->katalogi[$x]).' obrazków</em></td> <td align="right"><a href="?go=getimg&feature=2&import='.$this->katalogi[$x].'">importuj</a></td></tr>';
  228. $cats++;
  229. }
  230. }
  231. if($cats==0) echo '<tr><td>Nie wykryto żadnego dodatkowego folderu prócz folderów skryptu.</td></tr>';
  232. echo '</tbody>
  233. </table>
  234. </div>';
  235. }
  236.  
  237.  
  238. /*****************************************/
  239. /* DODATKOWE FUNKCJE
  240. /*****************************************/
  241. function checkExtension($name) {
  242. if(in_array($name, get_loaded_extensions())) return true;
  243. else return false;
  244. }
  245. function file_info($path) {
  246. $qpos = strpos($path, "?");
  247. if ($qpos!==false) $path = substr($path, 0, $qpos);
  248. $array = array(
  249. "extension" => pathinfo($path, PATHINFO_EXTENSION),
  250. "dirname" => pathinfo($path, PATHINFO_DIRNAME),
  251. "basename" => pathinfo($path, PATHINFO_BASENAME),
  252. "filename" => pathinfo($path, PATHINFO_FILENAME),
  253. );
  254. return $array;
  255. }
  256. function cat_list($katalog) {
  257. $dir = opendir($katalog);
  258. while ($file = readdir($dir)) {
  259. if (is_dir($katalog.'/'.$file) && $file != "." && $file != "..") {
  260. $this->katalogi[] = $file;
  261. }
  262. }
  263. }
  264. function numberOfPictures($katalog) {
  265. $file_types = array(1=>'jpg','jpeg','gif','png','JPG','JPEG','GIF','PNG');
  266. $dir = opendir($katalog);
  267. $files = 0;
  268. while(false !== ($file = readdir($dir))) {
  269. $info = pathinfo($file);
  270. if($file != '.' && $file != '..' && array_search($info['extension'],$file_types))
  271. $files++;
  272. }
  273. return $files;
  274. }
  275. function watermark($file, $watermark, $dir) {
  276. $info = pathinfo($file);
  277. if($info['extension'] != 'gif') {
  278. if(file_exists('lib/imageworkshop.lib.php')) {
  279. require_once('lib/imageworkshop.lib.php');
  280. $imageLayer = new ImageWorkshop(array(
  281. "imageFromPath" => $file,
  282. ));
  283. $watermarkLayer = new ImageWorkshop(array(
  284. "imageFromPath" => $watermark,
  285. ));
  286. } else {
  287. require_once('lib/imageworkshop/ImageWorkshop.php');
  288. $imageLayer = ImageWorkshop::initFromPath($file);
  289. $watermarkLayer = ImageWorkshop::initFromPath($watermark);
  290. }
  291. $imageLayer->addLayer(1, $watermarkLayer, 5, 5, "RB");
  292.  
  293. $createFolders = false;
  294. $backgroundColor = null;
  295. $imageQuality = 100;
  296.  
  297. $imageLayer->save($dir, $info['filename'].'.'.$info['extension'], $createFolders, $backgroundColor, $imageQuality);
  298. }
  299. }
  300.  
  301. function cropp($file, $px, $img_width, $img_height, $dir) {
  302. $info = pathinfo($file);
  303. if($info['extension'] != 'gif') {
  304. if(file_exists('lib/imageworkshop.lib.php')) {
  305. require_once('lib/imageworkshop.lib.php');
  306. $imageLayer = new ImageWorkshop(array(
  307. "imageFromPath" => $file,
  308. ));
  309. } else {
  310. require_once('lib/imageworkshop/ImageWorkshop.php');
  311. $imageLayer = ImageWorkshop::initFromPath($file);
  312. }
  313. $imageLayer->cropInPixel($img_width, $img_height-$px, 0, $px, 'LB');
  314.  
  315. $createFolders = false;
  316. $backgroundColor = null;
  317. $imageQuality = 100;
  318.  
  319. $imageLayer->save($dir, $info['filename'].'.'.$info['extension'], $createFolders, $backgroundColor, $imageQuality);
  320. }
  321. }
  322.  
  323. }
  324. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement