A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /* ===========================
  3. gelato CMS - A PHP based tumblelog CMS
  4. development version
  5. http://www.gelatocms.com/
  6. gelato CMS is a free software licensed under the GPL 2.0
  7. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  8. =========================== */
  9. ?>
  10. <?php
  11. function version() {
  12. return "0.85";
  13. }
  14. function beginsWith($str, $sub) {
  15. return (strpos($str, $sub) === 0);
  16. }
  17. function endsWith($str, $sub) {
  18. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  19. }
  20. function getFileName($fileUrl) {
  21. $path = explode('/', $fileUrl);
  22. return $path[count($path)-1];
  23. }
  24. function isMP3($fileUrl) {
  25. if (endsWith($fileUrl, ".mp3")) {
  26. return true;
  27. } else {
  28. return false;
  29. }
  30. }
  31. function getMP3File($remoteFileName) {
  32. if (isMP3($remoteFileName)) {
  33. if (getFile($remoteFileName)) {
  34. return true;
  35. } else {
  36. return false;
  37. }
  38. } elseif (isGoEar($remoteFileName)) {
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. function getGoEarCode($songUrl) {
  45. $pos = strpos($songUrl, "?v=");
  46. $lon = strlen($songUrl);
  47. $str = substr($songUrl, $pos + 3, $lon);
  48. return $str;
  49. }
  50. function isGoEar($songUrl) {
  51. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  52. return true;
  53. else
  54. return false;
  55. }
  56. function isImageFile($photoUrl) {
  57. if (endsWith($photoUrl, ".jpg")) { return true; }
  58. elseif (endsWith($photoUrl, ".gif")) { return true; }
  59. elseif (endsWith($photoUrl, ".png")) { return true; }
  60. else { return false; }
  61. }
  62. function getPhotoFile($remoteFileName) {
  63. if (isImageFile($remoteFileName)) {
  64. if (getFile($remoteFileName)) {
  65. return true;
  66. } else {
  67. return false;
  68. }
  69. } else {
  70. return false;
  71. }
  72. }
  73. function getFile($remoteFileName) {
  74. $fileName = sanitizeName("../uploads/".$remoteFileName);
  75. $str = _file_get_contents($remoteFileName);
  76. if (!$handle = fopen($fileName, 'w')) {
  77. return false;
  78. }
  79. if (fwrite($handle, $str) === FALSE) {
  80. return false;
  81. }
  82. fclose($handle);
  83. return true;
  84. }
  85. function isVimeoVideo($videoUrl) {
  86. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  87. return true;
  88. else
  89. return false;
  90. }
  91. function getVimeoVideoUrl($videoUrl) {
  92. return array_pop(explode("clip:",$videoUrl));
  93. }
  94. function isYoutubeVideo($videoUrl) {
  95. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  96. return true;
  97. else
  98. return false;
  99. }
  100. function getYoutubeVideoUrl($videoUrl) {
  101. $params = explode("?v=", $videoUrl);
  102. $params2 = explode("&",$params[1]);
  103. return $params2[0];
  104. }
  105. function isVideo($url) {
  106. if (isYoutubeVideo($url)) { return true; }
  107. elseif (isVimeoVideo($url)) { return true; }
  108. else { return false; }
  109. }
  110. function sendMail($to, $title, $body, $from) {
  111. $rp = trim($from);
  112. $org = "gelato CMS";
  113. $mailer = "gelato CMS Mailer";
  114. $head = '';
  115. $head .= "Content-Type: text/html \r\n";
  116. $head .= "Date: ". date('r'). " \r\n";
  117. $head .= "Return-Path: $rp \r\n";
  118. $head .= "From: $from \r\n";
  119. $head .= "Sender: $from \r\n";
  120. $head .= "Reply-To: $from \r\n";
  121. $head .= "Organization: $org \r\n";
  122. $head .= "X-Sender: $from \r\n";
  123. $head .= "X-Priority: 3 \r\n";
  124. $head .= "X-Mailer: $mailer \r\n";
  125. $body = str_replace("\r\n", "\n", $body);
  126. $body = str_replace("\n", "\r\n", $body);
  127. return @mail($to, $title, $body, $head);
  128. }
  129. function getThemes() {
  130. $themes_dir = "themes";
  131. $dirs = array();
  132. $path = getcwd();
  133. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  134. $dir = str_replace("admin\\", "", $dir);
  135. $dir = str_replace("admin/", "", $dir);
  136. $handle = opendir($dir);
  137. $i=0;
  138. while($filename = readdir($handle)) {
  139. if($filename != "." && $filename != "..") {
  140. $dirs[$i]=trim($filename);
  141. $i++;
  142. }
  143. }
  144. closedir($handle);
  145. return $dirs;
  146. }
  147. function sanitizeName($name) {
  148. $name = preg_replace('/[\'"]/', '', $name);
  149. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  150. $name = trim($name, '-');
  151. $name = strtolower($name);
  152. //HACK: We need to rework the regular expression to allow the dot
  153. $ext = substr($name, strlen($name)-3, strlen($name));
  154. $body = substr($name, 0, strlen($name)-4);
  155. $name = $body.".".$ext;
  156. return $name;
  157. }
  158. function _file_get_contents($path) {
  159. // Modified function from:
  160. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  161. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  162. if (!preg_match('/^http/i', $path)) {
  163. if ($fp = fopen($path, 'r')) {
  164. return fread($fp, 1024);
  165. } else {
  166. return false;
  167. }
  168. } else {
  169. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  170. $ch = curl_init();
  171. $timeout = 5;
  172. curl_setopt ($ch, CURLOPT_URL, $path);
  173. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  174. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  175. $file_contents = curl_exec($ch);
  176. curl_close($ch);
  177. if (is_string($file_contents)) {
  178. return $file_contents;
  179. } else {
  180. return false;
  181. }
  182. } else {
  183. $data = parse_url($path);
  184. if (!$data['host'] || $data['scheme'] != "http") {
  185. return false;
  186. }
  187. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  188. if (!$f) {
  189. return false;
  190. }
  191. $q = "GET " . $data['path'] . "?" . $data['query'] . " HTTP/1.1\r\n";
  192. $q .= "Host: " . $data['host'] . "\r\n";
  193. $q .= "Connection: close\r\n";
  194. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  195. $recv = "";
  196. fwrite($f, $q);
  197. while (!feof($f)) {
  198. $recv .= fread($f, 1024);
  199. }
  200. $request = $q;
  201. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  202. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  203. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  204. if ($res[1][0] != "2") {
  205. return false;
  206. }
  207. } else {
  208. return false;
  209. }
  210. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  211. $tmp_body = $body;
  212. $new = "";
  213. $exit = false;
  214. while (!$exit) {
  215. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  216. $len = hexdec($res[1]);
  217. if ($len == "0") {
  218. $exit = true;
  219. break;
  220. }
  221. $new .= substr($tmp_body, strlen($res[0]), $len);
  222. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  223. } else {
  224. $exit = true;
  225. }
  226. }
  227. $body = $new;
  228. }
  229. return $body;
  230. }
  231. }
  232. }
  233. function get_curl_version() {
  234. $curl = 0;
  235. if (is_array(curl_version())) {
  236. $curl = curl_version();
  237. $curl = $curl['version'];
  238. } else {
  239. $curl = curl_version();
  240. $curl = explode(' ', $curl);
  241. $curl = explode('/', $curl[0]);
  242. $curl = $curl[1];
  243. }
  244. return $curl;
  245. }
  246. ?>